 

package com.zdkj.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.admin.api.dto.RoleChangeApproveDTO;
import com.zdkj.admin.api.dto.RoleChangeCreateDTO;
import com.zdkj.admin.api.dto.RoleChangeQueryDTO;
import com.zdkj.admin.api.dto.RoleChangeUpdateDTO;
import com.zdkj.admin.service.SysRoleChangeService;
import com.zdkj.common.core.constant.CommonConstants;
import com.zdkj.common.core.exceltools.MultipartFileToFile;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.admin.api.entity.SysRoleChange;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;


/**
 * 权限变更申请表
 *
 * @author haonan
 * @date 2020-01-13 17:07:11
 */
@RestController
@AllArgsConstructor
@RequestMapping("/role/change" )
@Api(value = "/role/change", tags = "权限变更申请管理")
public class SysRoleChangeController {

    private final SysRoleChangeService sysRoleChangeService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param roleChangeQueryDTO 权限变更申请表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getSysRoleChangePage(Page page, RoleChangeQueryDTO roleChangeQueryDTO) {
        return R.ok(sysRoleChangeService.queryByDataScope(page, roleChangeQueryDTO));
    }


    /**
     * 通过id查询权限变更申请表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(sysRoleChangeService.getById(id));
    }

    /**
     * 新增权限变更申请表
     * @param roleChangeCreateDTO 权限变更申请表
     * @return R
     */
    @ApiOperation(value = "新增权限变更申请表", notes = "新增权限变更申请表")
    @SysLog("新增权限变更申请" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('sys_role_change_add')" )
    public R save(@RequestBody RoleChangeCreateDTO roleChangeCreateDTO) {
        return R.ok(sysRoleChangeService.saveWithCheck(roleChangeCreateDTO));
    }

    /**
     * 修改权限变更申请表
     * @param roleChangeUpdateDTO 权限变更申请表dto
     * @return R
     */
    @ApiOperation(value = "修改权限变更申请表", notes = "修改权限变更申请表")
    @SysLog("修改权限变更申请表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('sys_role_change_edit')" )
    public R updateById(@RequestBody RoleChangeUpdateDTO roleChangeUpdateDTO) {
        SysRoleChange sysRoleChange = sysRoleChangeService.getById(roleChangeUpdateDTO.getId());

        Integer status = sysRoleChange.getStatus();

        if(CommonConstants.APPROVE_TO_DO.equals(status) || CommonConstants.APPROVE_DIS_AGREE.equals(status)){
            if(StringUtils.isNotEmpty(roleChangeUpdateDTO.getNewRole())){
                sysRoleChange.setNewRole(roleChangeUpdateDTO.getNewRole());
            }
            if(StringUtils.isNotEmpty(roleChangeUpdateDTO.getRemark())){
                sysRoleChange.setRemark(roleChangeUpdateDTO.getRemark());
            }
            return R.ok(sysRoleChangeService.updateById(sysRoleChange));
        }else{
            return R.failed("只有待提交或拒绝的审批才可以重新编辑");
        }
    }

    /**
     * 通过id删除权限变更申请表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除权限变更申请表", notes = "通过id删除权限变更申请表")
    @SysLog("通过id删除权限变更申请表" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('sys_role_change_del')" )
    public R removeById(@PathVariable Integer id) {
        Integer status = sysRoleChangeService.getById(id).getStatus();
        if(CommonConstants.APPROVE_TO_DO.equals(status)){
            return R.ok(sysRoleChangeService.removeById(id));
        }else{
            return R.failed("只有待提交审批才可以删除");
        }
    }

    /**
     * 提交审批意见
     * @param roleChangeApproveDTO
     * @return
     */
    @ApiOperation(value = "审批",notes = "提交审批意见(支持批量)")
    @SysLog("权限变更审批" )
    @PutMapping("/approve")
    @PreAuthorize("@pms.hasPermission('sys_role_change_approve')" )
    public R approve(@RequestBody RoleChangeApproveDTO roleChangeApproveDTO){
        return R.ok(sysRoleChangeService.approve(roleChangeApproveDTO));
    }

    @ApiOperation(value = "批量导入权限变更申请",notes = "导入excel批量提交权限变更申请")
    @SysLog("批量导入权限变更")
    @PostMapping("/saveBatch")
    @PreAuthorize("@pms.hasPermission('sys_role_change_excel_batch_import')" )
    public R saveBatch(@RequestParam("file") MultipartFile file) throws Exception {
        File excelFile = MultipartFileToFile.multipartFileToFile(file);
        return R.ok(sysRoleChangeService.saveBatchWithExcel(excelFile));
    }

    @ApiOperation(value = "提交审批",notes = "提交审批")
    @SysLog("提交权限变更申请")
    @PutMapping("/submit/{id}")
    @PreAuthorize("@pms.hasPermission('sys_role_change_submit')" )
    public R submit(@PathVariable Integer id){
        SysRoleChange sysRoleChange = sysRoleChangeService.getById(id);
        sysRoleChange.setStatus(CommonConstants.APPROVE_DOING);
        return R.ok(sysRoleChangeService.updateById(sysRoleChange));
    }

}
